﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Serilog;
using Serilog.Core;
using SimpleTool.Base;
using SimpleTool.Server;

namespace SimpleTool.Capacity
{
    class CapacityViewModel : Notify
    {
        public IDirFileServer DirFileServer;
        public CapacityViewModel(IPowerPlantServer PowerServer, IDirFileServer dirFileServer)
        {
            PowerPlantServer = PowerServer;
            HoldCapacitySectionModel.MaxHeightValue = HoldCapacitySectionModel.AcreageMap.Keys.ToList().Max();
            HeightValueChangeEvent(HoldCapacitySectionModel.HeightValuePipeLine);
            DirFileServer = dirFileServer;
            ModelUpdate();
        }

        public IPowerPlantServer PowerPlantServer;

        private readonly ILogger Logger = Log.ForContext<CapacityViewModel>();

        private CapacitySectionModel? CapacityDataModelTemplate;

        private ICommand? CmdHeightChangeStdin;

        public CapacitySectionModel HoldCapacitySectionModel
        {
            set
            {
                CapacityDataModelTemplate = value;
                propertyChangeSend(nameof(HoldCapacitySectionModel));
            }
            get
            {
                CapacityDataModelTemplate ??= new CapacitySectionModel();
                return CapacityDataModelTemplate;
            }
        }

        public ICommand CmdHeightChange
        {
            set
            {
                CmdHeightChangeStdin = value;
            }
            get
            {
                CmdHeightChangeStdin ??= new EasyCommand(HeightValueChangeEvent, bool () => { return true; });
                return CmdHeightChangeStdin;
            }
        }

        public void HeightValueChangeEvent(object? obj)
        {
            if (obj != null)
            {
                if (int.TryParse(obj.ToString(), out int key) && HoldCapacitySectionModel.AcreageMap.TryGetValue(key, out double acreage))
                HoldCapacitySectionModel.VolumeValuePipeLine = acreage;
            }
            ModelUpdate();
        }

        public void SerialDataReceive(string conent)
        {
            Application.Current.Dispatcher.Invoke(async () =>
            {
                MatchCollection matchs = RegExp.RegExpNumber().Matches(conent);
                foreach (Match match in matchs)
                {
                    if (int.TryParse(match.Value, out int heihgt))
                    {
                        HoldCapacitySectionModel.HeightValuePipeLine = heihgt;
                    }
                    ModelUpdate();
                    await Task.Delay(20);
                }
            });
        }

        public void ModelUpdate()
        {
            propertyChangeSend(nameof(HoldCapacitySectionModel));
        }

    }
}
